(function (window) {
    function Lyric(path) {
        return new Lyric.prototype.init(path);
    }
    Lyric.prototype = {
        constructor:Lyric,
        init: function (path) {
            this.path = path;
        },
        times:[],
        lyrics:[],
        index:-1,
        loadLyric:function (callBack) {
            var $this = this;
            $.ajax({
                url:$this.path,
                dataType:"text",
                success:function (res) {
                    $this.parseLyric(res);
                    callBack();
                },
                error:function (e) {
                    console.log(e);
                }
            })
        },
        parseLyric:function (res) {
            var $this = this;
            //清空上一首歌词
            $this.times = [];
            $this.lyrics = [];
            var array = res.split("\n");
            // console.log(array);
            // [00:00.92]
            // 正则表达式 查询时间
            var timeReg = /\[(\d*:\d*\.\d*)\]/;
            //遍历取出每一行歌词
            $.each(array,function (index, ele) {
                //处理歌词
                var lrc = ele.split("]")[1];
                //排除空字符串(没有歌词的)
                if (lrc.length == 1) return true;
                $this.lyrics.push(lrc);

                var res =timeReg.exec(ele);

                if(res == null) return true;
                var timeStr = res[1];  //00:0092
                var res2 = timeStr.split(":");
                var min = parseInt(res2[0]) * 60;
                var sec = parseFloat(res2[1]);

                var time = parseFloat(Number(min+sec).toFixed(2));
                $this.times.push(time);

            });
        },

        currentIndex: function (currentTime) {
            console.log(currentTime);
            if(currentTime >= this.times[0]){
                this.index++;
                this.times.shift(); //shift删除数组最前面的一个元素
            }
            return this.index;
        }

    };
    Lyric.prototype.init.prototype = Lyric.prototype;
    window.Lyric =Lyric;
})(window);